package com.gzu;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 验证用户名和密码
        if ("zrz".equals(username) && "1".equals(password)) {
            HttpSession session = request.getSession();
            session.setAttribute("user", username);
            System.out.println("Login successful for user: " + username); // Debug info
            response.sendRedirect(request.getContextPath() + "/welcome.html"); // 登录成功后重定向
        } else {
            System.out.println("Login failed for user: " + username); // Debug info
            response.sendRedirect(request.getContextPath() + "/login.html?error=Invalid username or password");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 重定向到登录页面
        response.sendRedirect("login.html");
    }
}